package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //套餐表加入数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        //套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDishe -> setmealDishe.setSetmealId(setmealId));//遍历list，设置id
            log.info("套餐菜品关系表：{}",setmealDishes);
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    public PageResult querySetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询：{}",setmealPageQueryDTO);
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.getSetmealAll(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public void deleteSetmeal(List<Long> ids) {
        log.info("删除套餐：{}",ids.size());
        //判断ids是否为空
        if (ids == null || ids.size() == 0) {
            throw new BaseException(MessageConstant.ORDER_NOT_FOUND);
        }

        //判断是否有套餐在售卖
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        return setmealVO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //添加套餐菜品关联表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDishe -> setmealDishe.setSetmealId(setmeal.getId()));//遍历list，设置id
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        setmeal.setStatus(status);
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public List<DishItemVO> getSetmealByType(Integer categoryId) {
        List<DishItemVO> list = setmealMapper.getSetmealByType(categoryId);
        return list;
    }
    @Override
    public List<Setmeal> getSetmealByCategoryId(Integer categoryId) {
        List<Setmeal> list = setmealMapper.getSetmealByCategoryIdWithStatus(categoryId);
        return list;
    }
}
